home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7879 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  56 lines

  1. Newsgroups: comp.lang.c++
  2. Path: uu4news.netcom.com!amc-gw!curtis
  3. From: curtis@amc.com (Curtis Green)
  4. Subject: Re: cin.get(string) problem
  5. Message-ID: <1996Feb14.011237.14914@amc.com>
  6. Organization: Applied Microsystems Corporation
  7. X-Newsreader: TIN [version 1.1 PL6]
  8. References: <4fk02a$nh@reader2.ix.netcom.com>
  9. Date: Wed, 14 Feb 1996 01:12:37 GMT
  10.  
  11. Kurt W. Greiner (greinerk@ix.netcom.com) wrote:
  12. : Hi all,
  13. :         i was trying some really simple io for a program that just gets 
  14. : values, here is a sample of code
  15. : #include <iostream.h>
  16. : void main(void)
  17. : {
  18. :         char string[80];
  19. :         cin.get(string,sizeof(string));
  20. :         cin.get(string,sizeof(string));
  21. : }
  22. : ok, say i want to read in a name on the first cin, i type in "john smith" and 
  23. : the prompt, john gets thrown in to the first and smith the second, i do not 
  24. : even get prompted for the second cin. it does not happen if i type in a string 
  25. : with out spaces. i am using borland c++ 2.0, am i doing something wrong or is 
  26. : there an other way of doing this other than gets(string)?
  27. : kurt
  28.  
  29. Might be a problem with whitespace.  "john smith" has a whitespace delimiter
  30. between "john" and "smith", to the get method sees the space and quits input.
  31. Try getline().  that should default to a '\n' delimiter.
  32.  
  33. void main(void)
  34. {
  35.     char string[80];
  36.     cin.getline(string,sizeof(string));
  37.     cin.getline(string,sizeof(string));
  38. }
  39.  
  40. will do what you want 
  41.  
  42.  
  43. --
  44. Be seeing you...
  45.  
  46. Curtis Green    |    Software Engineer
  47. curtis@amc.com    |    Applied Microsystems Embedded Systems
  48.                 |    http://www.amc.com
  49. My opinions are |
  50. expressly mine  |    This year Khan escapes Earth on the Botany Bay.
  51. on my own.      |    (Cliff Clavin, 1996)
  52.